package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ItemInventoryScanBinding;

import java.util.List;

/**
 * @author xubin
 */
public class InventoryScanAdapter extends BaseQuickAdapter<ProductBean, BaseDataBindingHolder<ItemInventoryScanBinding>> {

    private String storehouseLocatName;
    private String sortName;

    public InventoryScanAdapter(int layoutResId, @Nullable List<ProductBean> data) {
        super(layoutResId, data);
    }

    public void setStorehouseLocatName(String storehouseLocatName) {
        this.storehouseLocatName = storehouseLocatName;
    }

    public String getStorehouseLocatName(ProductBean item) {
        String storehouseLocatName = item.getStorehouseLocatName();
        if (!TextUtils.isEmpty(storehouseLocatName)) {
            return storehouseLocatName;
        }
        return this.storehouseLocatName;
    }

    public void setSortName(String sortName) {
        this.sortName = sortName;
    }

    public void setViewData(@IntRange(from = 0) int index, Object payload) {
        notifyItemChanged(index + getHeaderLayoutCount(), payload);
    }

    private int getInventoryResult(ProductBean item) {
        Integer inventoryResult = item.getInventoryResult();
        if (inventoryResult == null) {
            return R.drawable.ic_check_right_15dp;
        }
        switch (inventoryResult) {
            case ProductBean.TYPE_DEFICIT:
                //盘亏
                return R.drawable.ic_check_error_15dp;
            case ProductBean.TYPE_PANYING:
                //待盘
                return R.drawable.ic_panying_15dp;
            case ProductBean.TYPE_UNKNOWN:
                //移入该位置
                return R.drawable.ic_inventory_unknown_15dp;
            default:
                return R.drawable.ic_check_right_15dp;
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(@NonNull BaseDataBindingHolder<ItemInventoryScanBinding> bindingHolder, ProductBean item) {
        ItemInventoryScanBinding dataBinding = bindingHolder.getDataBinding();
        if (dataBinding == null) {
            return;
        }
        dataBinding.tvTitle.setText(sortName);
        dataBinding.tvModel.setText("编号：" + item.getProductCode());
        dataBinding.tvLoc.setText("位置：" + getStorehouseLocatName(item));
        dataBinding.ivType.setVisibility(item.getInventoryResult() == null ? View.INVISIBLE : View.VISIBLE);
        dataBinding.ivType.setImageResource(getInventoryResult(item));
        dataBinding.ivMenu.setOnClickListener(v -> {
            if (getOnItemClickListener() != null) {
                getOnItemClickListener().onItemClick(InventoryScanAdapter.this, v, bindingHolder.getAbsoluteAdapterPosition());
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull BaseDataBindingHolder<ItemInventoryScanBinding> holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        ItemInventoryScanBinding dataBinding = holder.getDataBinding();
        if (dataBinding == null) {
            return;
        }
        ProductBean item = getData().get(position - getHeaderLayoutCount());
        dataBinding.ivType.setVisibility(item.getInventoryResult() == null ? View.INVISIBLE : View.VISIBLE);
        dataBinding.ivType.setImageResource(getInventoryResult(item));
    }
}
